<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Asset Manager
 *
 * Software to automatically manage how assets are delivered on a website
 *
 * @package		Asset Manager
 * @author		Adam Lewis <adam.lewis@bestservedcold.com>
 * @copyright	Copyright (c) 2006 - 2012, Best Served Cold Media Limited.
 * @license		http://bestservedcold.com/license.html
 * @link		http://bestservedcold.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Single Query Assets Library
 *
 * The Asset library simply checks the asset database and returns the asset location.  If the asset
 * is not in the database, then it calls the asset library and queues the asset for processing.
 *
 * @package		Asset Manager
 * @subpackage	Assets
 * @category	Libraries
 * @author  	Adam Lewis <adam.lewis@bestservedcold.com>
 * @since		2012-05-22
 */
require_once(APPPATH.'libraries/assets/Am_optimize.php');
class Optimize_images extends Am_optimize {

	private $_asset;

	/**
	 * Constructor
	 *
	 * @access		public
	 * @since		2012-05-22
	 * @return		void
	 */
	public function __construct($asset)
	{
		parent::__construct($asset);
	}

	private function get_image_string($file)
	{
		return imagecreatefromstring(file_get_contents($this->get_absolute_location($file)));
	}

	public function optimize($quality = 90)
	{
		// Firstly, can we base64 this mother?
		if ($this->CI->config->item('base64_limit', 'asset_manager') &&
			$this->_asset['filesize'] <= $this->CI->config->item('base64_limit', 'asset_manager') &&
			in_array(pathinfo($this->_asset['file'], PATHINFO_EXTENSION), array('jpg', 'gif', 'png')))
		{
			return $this->base64_asset($this->_asset);
		}

		// Check that the file isn't already compressed
		if ($this->check_if_compressed($file))
		{
			return $asset;
		}

		// Get the new filename
		$new_file = $this->get_optimized_filename($file);

		// What's the current image string
		$image_string = $this->get_image_string($file);

		// Go through the different image files and optimize
		switch (pathinfo(strtolower($asset['file']), PATHINFO_EXTENSION))
		{
			case 'jpg':
			case 'jpeg':
				imagejpeg($this->get_image_string($file), $this->get_absolute_location($new_file),
					$this->CI->config->item('compress_images', 'assets'));
				break;
			case 'png':
				imagepng($this->get_image_string($file), $this->get_absolute_location($new_file),
					$this->CI->config->item('compress_images', 'assets'));
				break;
			case 'gif':
				$im = new Imagick($image_string);
				$im->optimizeImageLayers();
				$im->writeimage($this->get_absolute_location($new_file));
				break;

		}
		return $new_file;
	}


	/**
	 * Base64 Asset
	 *
	 * Simply converts the file to base64 and plonks that in the db
	 *
	 * @access		private
	 * @since		2012-05-25
	 * @param 		array		$asset
	 * @return		void
	 */
	private function base64_asset($asset)
	{
		$asset['base64'] = "data:{$asset['filetype']};base64,".
			base64_encode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$asset['file']));
		return $asset;
	}
}